#ifndef OTEXTURE_BLOCKS_H
#define OTEXTURE_BLOCKS_H

#include <map>

#include "Vertex.h"
#include "Texture.h"

#include "OGL.h"
#if defined(__APPLE__) || defined(MACOSX)
	   #include <OpenGL/OpenGL.h>
#endif
#if defined(_WIN32)
	#include <GL/gl.h>
#endif

namespace Y{

typedef Vertex< 2, Vertex2i > TexPartition;

struct TexCoordBlock{
	Vertex2f position;
	Vertex2f dimensions;
	
	Vertex2f one()		{ return Vertex2f(position.x(),position.y()); }
	Vertex2f two()		{ return Vertex2f(position.x() + dimensions.x(),position.y()); }
	Vertex2f three()	{ return Vertex2f(position.x() + dimensions.x(),position.y() + dimensions.y()); }
	Vertex2f four()	{ return Vertex2f(position.x(),position.y() + dimensions.y()); }
};

struct rsorter{bool operator()(int a, int b) const{return a > b;}};

class TextureBlock{

	public:
		
		int width;
		int height;
		int gapSize;
		std::map< int,TexCoordBlock > blocks;
		std::multimap< int,TexPartition,rsorter > free_partitions;
		GLuint texID;
		
		static bool compressTextures;

	public:
	
		TextureBlock (int pw, int ph, int gap_size = 0);
		
		bool addTexture (Texture*t);
		bool addTextures ( std::vector< Texture* >& t ) ;
		bool removeTexture (Texture*t);
		bool containsTexture (Texture*t){ return (blocks.find(t->yID) != blocks.end()); }
		
		void clear ();
		
		TexCoordBlock texCoords(int yID){ return blocks[yID]; }
		TexCoordBlock texCoords(Texture*t){ return blocks[t->yID]; }
		
		void consolidatePartitions ();

};

}

#endif
